package cn.bluedot.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.bluedot.dao.EquMsgShowDao;
import cn.bluedot.model.Condition;
import cn.bluedot.model.ConditionList;
import cn.bluedot.model.Equipment;
import cn.bluedot.model.EquipmentExceptionVo;
import cn.bluedot.model.ExcCondition;
import cn.bluedot.model.MonitorVedio;
import cn.bluedot.model.SensorDataVo;
import cn.bluedot.model.TableBodyList;
import cn.bluedot.model.VideoCondition;
import cn.bluedot.model.VideoConditionList;
import cn.bluedot.service.EquMsgShowService;

/**
*
* @author 余聪
* @time:2019年4月17日下午7:50:40
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-mybatis.xml")
@Service
public class EquMsgShowServiceImpl implements EquMsgShowService{
	
	@Autowired
	EquMsgShowDao equMsgShowDao;


	@Override
	public List<Equipment> selectAllEqu() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public ConditionList selectConditionList(Condition condition, Integer userId) {
		ConditionList conditionList = new ConditionList();
		if(condition.getEquType() == null || "".equals(condition.getEquType())){
			conditionList.setEquTypeList(equMsgShowDao.selectEquTypeList());
			conditionList.setEquNameList(equMsgShowDao.selectEquNameList("传感器"));
		}else if(condition.getEquType() != null && condition.getEquType().length() > 0
				&& (condition.getEquName() == null || condition.getEquName().length() == 0)){
			conditionList.setEquTypeList(equMsgShowDao.selectEquTypeList());
			conditionList.setEquNameList(equMsgShowDao.selectEquNameList(condition.getEquType()));
		}else if(condition.getEquName() != null && condition.getEquName().length() > 0
				&& (condition.getPigstyId() == null || condition.getPigstyId() == 0)){
			conditionList.setEquTypeList(equMsgShowDao.selectEquTypeList());
			conditionList.setEquNameList(equMsgShowDao.selectEquNameList(condition.getEquType()));
			conditionList.setPigstyIdList(equMsgShowDao.selectPigstyIdList(userId));
		}else{
			conditionList.setEquTypeList(equMsgShowDao.selectEquTypeList());
			conditionList.setEquNameList(equMsgShowDao.selectEquNameList(condition.getEquType()));
			conditionList.setPigstyIdList(equMsgShowDao.selectPigstyIdList(userId));
			conditionList.setHogcoteIdList(equMsgShowDao.selectHogcoteIdList(condition.getPigstyId()));
		}
		return conditionList;
	}


	@Override
	public TableBodyList selectTableBodyList(Condition condition, int i) {
		condition.setPageIndex((condition.getPageIndex()-1)*10);
		if(condition.getEquType()==null || condition.getEquType().length() == 0 || condition.getEquType().startsWith("all")){
			condition.setEquType("传感器");
		}
		if(condition.getEquName()==null || condition.getEquName().length() == 0 || condition.getEquName().startsWith("all")){
			condition.setEquName("");
		}
		if(condition.getHogcoteId()==null || condition.getHogcoteId() == 0){
			condition.setHogcoteId(null);
		}
		if(condition.getPigstyId()==null || condition.getPigstyId() == 0){
			condition.setPigstyId(null);
		}
		TableBodyList tableBodyList = new TableBodyList();
		switch (condition.getEquType()) {
		case "传感器":
			tableBodyList.setSensorVoList(equMsgShowDao.selectSensorVo(condition));
			break;
		case "环控设备":
			tableBodyList.setControlEquVoList(equMsgShowDao.selectControlEquVo(condition));
			break;
		case "饲喂设备":
			tableBodyList.setFeedEquVoList(equMsgShowDao.selectFeedEquVo(condition));
			break;
		case "监控设备":
			tableBodyList.setMonitorEquVoList(equMsgShowDao.selectMonitorEquVo(condition));
			break;
		default:
			tableBodyList.setSensorVoList(equMsgShowDao.selectSensorVo(condition));
			break;
		}
		return tableBodyList;
	}


	@Override
	public Map<Integer, List<SensorDataVo>> selectSensorData(Integer[] sensorIds,
			Date startTime, Date endTime) {
		Map<Integer, List<SensorDataVo>> sensorData = new HashMap<Integer, List<SensorDataVo>>();
		for(int i = 0; i < sensorIds.length; i++){
			endTime.setDate(endTime.getDate()+1);;
			sensorData.put(sensorIds[i], equMsgShowDao.selectSensorData(sensorIds[i],startTime,endTime));
		}
		return sensorData;
	}
	
	@Test
	public void test1(){
		SensorDataVo sensorDataVo = equMsgShowDao.selectSensorDataById(1);
		/*SensorData sensorData = new SensorData();
		sensorData.setEquId(1);
		sensorData.setGetTime(new Date());
		sensorData.setSensorData(20.8f);
		List<Float> list = equMsgShowDao.selectSensorData(1,null,sensorData.getGetTime());
		equMsgShowDao.insertSensorData(sensorData);*/
		System.out.println(sensorDataVo.getGetTime());
	}

	@Override
	public void updateEquipment(Equipment equipment) {
		equMsgShowDao.updateEquipment(equipment);
	}


	@Override
	public Integer selectTableBodyTotalPage(Condition condition, int i) {
		Integer totalCount = equMsgShowDao.selectTableBodyTotalCount(condition);
		Integer totalPage = (totalCount/10==0)?(totalCount/10):(totalCount/10+1);
		return totalPage;
	}


	@Override
	public List<Float> getSensorNewData(Integer[] sensorIds) {
		List<Float> newDataList = new ArrayList<Float>();
		for(int i = 0; i < sensorIds.length; i++){
			newDataList.add(equMsgShowDao.getSensorNewData(sensorIds[i]));
		}
		return newDataList;
	}


	@Override
	public List<EquipmentExceptionVo> selectEquExcList(ExcCondition excCondition,
			int i) {
		excCondition.setPageIndex((excCondition.getPageIndex()-1)*10);
		if(excCondition.getEquType()==null || excCondition.getEquType().length() == 0 || excCondition.getEquType().startsWith("all")){
			excCondition.setEquType("传感器");
		}
		if(excCondition.getEquName()==null || excCondition.getEquName().length() == 0 || excCondition.getEquName().startsWith("all")){
			excCondition.setEquName("");
		}
		if(excCondition.getHogcoteId()==null || excCondition.getHogcoteId() == 0){
			excCondition.setHogcoteId(null);
		}
		if(excCondition.getPigstyId()==null || excCondition.getPigstyId() == 0){
			excCondition.setPigstyId(null);
		}
		List<EquipmentExceptionVo> equExcList = equMsgShowDao.selectEquipmentExceptionVo(excCondition);
		
/*		switch (condition.getEquType()) {
		case "传感器":
			tableBodyList.setSensorVoList(equMsgShowDao.selectSensorVo(condition));
			break;
		case "环控设备":
			tableBodyList.setControlEquVoList(equMsgShowDao.selectControlEquVo(condition));
			break;
		case "饲喂设备":
			tableBodyList.setFeedEquVoList(equMsgShowDao.selectFeedEquVo(condition));
			break;
		case "监控设备":
			tableBodyList.setMonitorEquVoList(equMsgShowDao.selectMonitorEquVo(condition));
			break;
		default:
			tableBodyList.setSensorVoList(equMsgShowDao.selectSensorVo(condition));
			break;
		}*/
		return equExcList;
	}


	@Override
	public Integer selectEquExcTotalPage(ExcCondition excConditione, int i) {
		Integer totalCount = equMsgShowDao.selectEquExcTotalCount(excConditione);
		Integer totalPage = (totalCount%10==0)?(totalCount/10):(totalCount/10+1);
		return totalPage;
	}


	@Override
	public VideoConditionList selectVideoConditionList(
			VideoCondition videoCondition, Integer userId) {
		VideoConditionList videoConditionList = new VideoConditionList();
		if(videoCondition.getPigstyId() == null || videoCondition.getPigstyId() == 0){
			videoConditionList.setPigstyIdList(equMsgShowDao.selectPigstyIdList(userId));
			videoConditionList.setEquIdList(equMsgShowDao.selectMonitorIdList(null,null));
		}else if((videoCondition.getPigstyId() != null || videoCondition.getPigstyId() > 0)
				&& (videoCondition.getHogcoteId() == null || videoCondition.getHogcoteId() == 0)){
			videoConditionList.setPigstyIdList(equMsgShowDao.selectPigstyIdList(userId));
			videoConditionList.setHogcoteIdList(equMsgShowDao.selectHogcoteIdList(videoCondition.getPigstyId()));
			videoConditionList.setEquIdList(equMsgShowDao.selectMonitorIdList(videoCondition.getPigstyId(),null));
		}else{
			videoConditionList.setPigstyIdList(equMsgShowDao.selectPigstyIdList(userId));
			videoConditionList.setHogcoteIdList(equMsgShowDao.selectHogcoteIdList(videoCondition.getPigstyId()));
			videoConditionList.setEquIdList(equMsgShowDao.selectMonitorIdList(videoCondition.getPigstyId(),videoCondition.getHogcoteId()));
		}
		return videoConditionList;
	}


	@Override
	public List<MonitorVedio> selectEquVideoList(VideoCondition videoCondition) {
		List<MonitorVedio> equVideoList = equMsgShowDao.selectEquVideoList(videoCondition);
		return equVideoList;
	}
	   
}
